import { ENV } from '.env.js'

// 请求拦截器
uni.addInterceptor('request', {
  invoke(args) {
		if(args.url.indexOf('https://') === -1 && args.url.indexOf('http://') === -1) {
			args.url = ENV.BASE_API + args.url
		}
  },
  success(args) {
    // console.log('success', args)
  },
  fail(err) {
    // console.log('interceptor-fail',err)
  },
  complete(res) {
    // console.log('interceptor-complete',res)
  }
})

// 请求函数
function request(config) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: config.url,
      data: config.data || {},
      header: {
        [ENV.HEAD]: uni.getStorageSync('token') || '',
        'content-type': config.contentType || 'application/json;charset=utf-8',
        ...config.header,
      },
      method: config.method || 'POST',
      dataType: config.dataType || 'JSON',
      success: async (res) => {
        // #ifdef APP-PLUS || H5
        var data = res.data
        // #endif
        // #ifdef MP-WEIXIN
        var data = JSON.parse((res.data))
        // #endif
        if(data.code && data.code != 200){
          console.log(data);
          uni.hideLoading()
          if(data.code == 401) {
            uni.removeStorageSync('token')
            uni.removeStorageSync('userInfo')
            uni.showToast({
              icon: 'none',
              title: '登录超时，请重新登录',
              success: () => {
                setTimeout(() => {
                  uni.setStorageSync('noLogin', '果然年代')
                  uni.setStorageSync("invitationUrl", window.location.pathname + window.location.search);
                  uni.reLaunch({
                    url: '/pages/index/index'
                  })
                }, 800)
              }
            })
          } else {
            if(data.code == 500 && data.msg === null) {
              uni.removeStorageSync('token')
              uni.removeStorageSync('userInfo')
              uni.showToast({
                icon: 'none',
                title: '登录超时，请重新登录',
                success: () => {
                  setTimeout(() => {
					          uni.setStorageSync('noLogin', '果然年代')
                    uni.setStorageSync("invitationUrl", window.location.pathname + window.location.search);
                    uni.reLaunch({
                      url: '/pages/index/index'
                    })
                  }, 800)
                }
              })
            } else if(data.code == 500 && data.msg === '微信服务错误:  微信登录超时 请重新登录 ') {
              uni.showToast({
                icon: 'none',
                title: '微信自动登录失败，请手动登录',
                success: () => {
                  setTimeout(() => {
					          uni.setStorageSync('noLogin', '果然年代')
                    uni.setStorageSync("invitationUrl", window.location.pathname + window.location.search);
                    uni.reLaunch({
                      url: '/pages/index/index'
                    })
                  }, 800)
                }
              })
            } else if(data.code == 500 && data.msg === '登录过期，请重新登录！') {
              uni.removeStorageSync('token')
              uni.removeStorageSync('userInfo')
              uni.showToast({
                icon: 'none',
                title: '登录超时，请重新登录',
                success: () => {
                  setTimeout(() => {
                  uni.setStorageSync('noLogin', '果然年代')
                  uni.setStorageSync("invitationUrl", window.location.pathname + window.location.search);
                  uni.reLaunch({
                      url: '/pages/index/index'
                    })
                  }, 800)
                }
              })
            } else {
              if(!data.msg) {
                data.msg = '系统内部错误，请联系管理员'
              }
              uni.showToast({
                icon: 'none',
                title: data.msg,
              })
            }
          }
          setTimeout(() => {
            reject()
          }, 1000)
          return
        }
        resolve(data)
      },
      fail: (err) => {
        uni.showToast({
          icon: 'none',
          title: '接口请求失败'
        })
        uni.hideLoading()
        reject(err)
      },
      complete: (res) => {
        try {
          config.complete(res)
        } catch (e) {
          // TODO handle the exception
        }
      }
    })
  })
}

export default request
